﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Repositories.Interfaces;
using Repositories;
using System.Data;
using System.Globalization;

namespace AsiaSnackExpress
{
    public partial class EditCombo : System.Web.UI.Page
    {
        #region Properties



        IComboRepository _comboRep;
        public IComboRepository ComboRep
        {
            get
            {
                if (_comboRep == null)
                {
                    _comboRep = DIHelper.Resolve<IComboRepository>();
                }
                return _comboRep;
            }

        }

        public int ComboId
        {
            get
            {
                string comboId = Request.QueryString["id"];
                if (string.IsNullOrEmpty(comboId))
                    return 0;
                return int.Parse(comboId);
            }
        }

        Combo current = null;
        public Combo CurrentCombo
        {
            get
            {
                if (current == null)
                    current = new Combo();
                current.EnglishName = txtNameEnglish.Text.Trim();
                current.Name = txtName.Text.Trim();
                current.Description = txtDescription.Text.Trim().Replace(Environment.NewLine, "<br/>");
                current.EnglishDescription = txtDescrioptionEnglish.Text.Trim().Replace(Environment.NewLine, "<br/>");
                current.Meals = txtMeals.Text.Trim().Replace(Environment.NewLine, "<br/>");
                current.EnglishMeals = txtMealEnglish.Text.Trim().Replace(Environment.NewLine, "<br/>");
                if (!string.IsNullOrEmpty(txtPrice.Text.Trim()))
                    current.Price = float.Parse(txtPrice.Text.Trim());
                else
                    current.Price = null;
                return current;
            }
            set
            {
                if (value != null)
                {
                    txtNameEnglish.Text = value.EnglishName;
                    txtName.Text = value.Name;
                    txtDescription.Text = value.Description.Replace("<br/>", Environment.NewLine);
                    txtDescrioptionEnglish.Text = value.EnglishDescription.Replace("<br/>", Environment.NewLine);
                    txtMeals.Text = value.Meals.Replace("<br/>", Environment.NewLine);
                    txtMealEnglish.Text = value.EnglishMeals.Replace("<br/>", Environment.NewLine);
                    if (value.Price != null)
                        txtPrice.Text = value.Price.Value.ToString();
                    if (!string.IsNullOrEmpty(value.Image))
                    {
                        imageOld.ImageUrl = string.Format("/images/{0}", value.Image);
                    }
                }
            }
        }


        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            DIHelper.Configure();
            base.OnLoad(e);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            //Check loock in
            ComboRep.Refresh();
            if (!IsPostBack)
            {
                CurrentCombo = ComboRep.FindById(ComboId);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var combo = ComboRep.FindById(ComboId);
                if (combo != null)
                {
                    combo.Name = CurrentCombo.Name;
                    combo.EnglishName = CurrentCombo.EnglishName;
                    combo.Description = CurrentCombo.Description;
                    combo.EnglishDescription = CurrentCombo.EnglishDescription;
                    combo.Meals = CurrentCombo.Meals;
                    combo.EnglishMeals = CurrentCombo.EnglishMeals;
                    combo.Price = CurrentCombo.Price;

                    if (fileImage.HasFile)
                    {
                        fileImage.SaveAs(Server.MapPath("~/images/") + fileImage.FileName.ToString());
                    }
                    combo.Image = fileImage.FileName.ToString();
                    ComboRep.Update(combo);
                    ComboRep.ApplyTheChanges();
                    lblMessage.Text = "Combo Saved!";
                    CurrentCombo = combo;
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "The combo is not found.";
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }

        #endregion


    }
}